package com.societyGames.flashForms
{
	import com.societyGames.system.EventObject;
	
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;

	//Scale only if we start to run out of room.	
	public class HeadersContainer extends Sprite //implements IChangeSize
	{
		//private var _sizeChangedEventObject:EventObject;
		private var _headers:Array = []; //DisplayObject
		private var _desiredHeight:Number;
		private var _desiredWidth:Number;
		
		private var _selectedHeader:DisplayObject;
		private var _headerHeight:Number = 0; //The size of the tallest header
		private var _nextHeaderX:Number = 0; //How far we're push to the right.
		private var _scrollRect:Rectangle = new Rectangle();
		private var _tabLine:DisplayObject;
		
		public function HeadersContainer(tabLine:DisplayObject)
		{
			this._tabLine = tabLine;
			this.addChild(this._tabLine);
			//this._sizeChangedEventObject = new EventObject(this, "SIZE_CHANGED");
		}
		
		/*public function get sizeChangedEventObject():EventObject
		{
			return this._sizeChangedEventObject;
		}*/
		
		public function get headerHeight():Number
		{
			return this._headerHeight;
		}
		
		public override function set height(value:Number):void
		{
			if (value != this._desiredHeight)
			{
				this._desiredHeight = value;
				recalculateHeight();
			}
		}
		
		public override function set width(value:Number):void
		{	
			if (value != this._desiredWidth)
			{
				this._desiredWidth = value;
				recalculateWidth();
			}
		}
		
		public function addHeader(header:DisplayObject):void
		{
			//Add item to main list
			this._headers.push(header);
			
			//Add to render list
			this.addChildAt(header, 0);
			
			//Select if there isn't one.
			if (this._selectedHeader == null)
			{
				selectHeader(header);	
			}
			
			//Line up all the contents and tabs correctly.
			recalculateAfterHeadersChanged();
		}
		
		public function selectHeader(header:DisplayObject):void
		{
			if (this._selectedHeader != header)
			{					
				if (header != null)
				{
					//Move tabline to second to front.
					this.removeChild(this._tabLine);
					this.addChild(this._tabLine);
					//Move selected tab past it to the front.
					this.removeChild(header);
					this.addChild(header);
				}
				this._selectedHeader = header;
			}
		}

		public function removeHeader(header:DisplayObject):void
		{
			//See if the tab is present.	
			var index:int = this._headers.indexOf(header);
			if (index != -1)
			{
				//remove from main list.
				this._headers.splice(index, 1);
				
				//Stop rendering
				this.removeChild(header);
				
				//Rearrange everything
				recalculateAfterHeadersChanged();
			}
		}
		
		private function recalculateAfterHeadersChanged():void
		{
			//Find the max height
			this._headerHeight = 0;			
			for each (var header:DisplayObject in this._headers)
			{
				if (header.height > this._headerHeight)
				{
					this._headerHeight = header.height;
				}
			}
			//Adjust position
			this._nextHeaderX = 0;
			for each (header in this._headers)
			{	
				header.x = this._nextHeaderX;
				this._nextHeaderX += header.width;
				header.y = this._headerHeight - header.height;
			}
			
			this._tabLine.y = this._headerHeight - this._tabLine.height;
			
			recalculateWidth();
			recalculateHeight();
		}
		
		private function recalculateWidth():void
		{
			/*this.scaleX = 1;
			if (this._desiredWidth < this._nextHeaderX)
			{
				this.scaleX = this._desiredWidth / this._nextHeaderX;
			}*/
			
			this._scrollRect.width = this._desiredWidth;
			this.scrollRect = this._scrollRect;
			
			this._tabLine.width = this._desiredWidth;
		}
		
		private function recalculateHeight():void
		{
			//Get back to default scale before calculating anything. 
			//this.scaleY = 1;
			//Push all the headers down to the bottom.
			/*for each (var header:DisplayObject in this._headers)
			{
				header.y = this._headerHeight - header.height;
			}*/
			//Scale down if we have to.
			/*if (this._desiredHeight < this._headerHeight)
			{
				this.scaleY = this._desiredHeight / this._headerHeight;
			}*/
			this._scrollRect.height = this._desiredHeight;
			this.scrollRect = this._scrollRect;
		}
	}
}